package com.dxyxx.framework;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.customized.ExtendedViewPager;
import com.android.customized.TouchImageView;
import com.dxyxx.R;

public class ViewPagerExampleActivity extends BaseActivity {

	/**
	 * Step 1: Download and set up v4 support library:
	 * http://developer.android.com/tools/support-library/setup.html Step 2:
	 * Create ExtendedViewPager wrapper which calls
	 * TouchImageView.canScrollHorizontallyFroyo Step 3: ExtendedViewPager is a
	 * custom view and must be referred to by its full package name in XML Step
	 * 4: Write TouchImageAdapter, located below Step 5. The ViewPager in the
	 * XML should be ExtendedViewPager
	 */
	private TextView text;
	private int index;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_viewpager_example);
		images = getIntent().getStringExtra("images").split(";");
		//  如果有输入 index.. 没有输入，默认显示第一张
		index = getIntent().getIntExtra("index", 0);//从0开始

		ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
		text = (TextView) findViewById(R.id.indexText);
		//		text.setText("1/" + images.length);
		text.setText((index+1) + "/" + images.length);
		ImageView backImageView = (ImageView) findViewById(R.id.backImageView);
		backImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				text.setText((arg0 + 1) + "/" + images.length);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		mViewPager.setAdapter(new TouchImageAdapter());
		mViewPager.setCurrentItem(index);// 当前页..从0 开始
	}

	private String[] images = {};

	class TouchImageAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return images.length;
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			TouchImageView img = new TouchImageView(container.getContext());
			imageLoader.displayImage(baseImageURL + images[position].split("_")[0] + "?p=0", img, squareOptions, animateFirstListener);

			//			img.setImageResource(images[position]);
			container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

			return img;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}
}
